//
//  postView.m
//  timemovie
//
//  Created by Yiqiao on 15/7/24.
//  Copyright (c) 2015年 Yiqiao. All rights reserved.
//

#import "postView.h"
#import "UIViewExt.h"
#import "posterCollectionView.h"
#import "indexCollectionView.h"
#define w [UIScreen mainScreen].bounds.size.width
#define h [UIScreen mainScreen].bounds.size.height
#define navH 64
#define barH 49
#define kHeaderViewH 136
#define kIndexCollectionViewH 100
#define kBottomTitleLableH 35
#define kBottomTitleLableY (h-barH-kBottomTitleLableH)
#define kHeaderOffH   36
#define kPosterColletionViewY  (kHeaderViewH-kHeaderOffH)
#define kPosterColletionViewH  (h-kPosterColletionViewY-barH-kBottomTitleLableH)
@implementation postView


- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self _createPostView];
        [self _createBottomLabel];
        [self _createCoverView];
        [self _createHeadView];
        [self _createGesture];
        [_indexColleeciton addObserver:self forKeyPath:@"currentIndex" options:NSKeyValueObservingOptionNew context:nil];
        [_postCollectionView addObserver:self forKeyPath:@"currentIndex" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    
    NSNumber *number = [change objectForKey:@"new"];
    NSInteger index = [number integerValue];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    if ([object isKindOfClass:[indexCollectionView class]] && _postCollectionView.currentIndex != index) {
        [_postCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition: UICollectionViewScrollPositionCenteredVertically animated:YES];
        _postCollectionView.currentIndex = index;
    }
    else if([object isKindOfClass:[posterCollectionView class]]&&_indexColleeciton.currentIndex!=index){
        [_indexColleeciton scrollToItemAtIndexPath:indexPath atScrollPosition: UICollectionViewScrollPositionCenteredVertically animated:YES];
        _indexColleeciton.currentIndex = index;
    }
    
    MObj *model  = _movieModelArray[index];
    _bottomTitleLabel.text = model.title;
}

- (void)_createHeadView{
    //mid
    _headView = [[UIView alloc]initWithFrame:CGRectMake(0, -kHeaderOffH, w, kHeaderViewH)];
    _headView.backgroundColor = [UIColor clearColor];
    UIImage *image = [UIImage imageNamed:@"indexBG_home"];
    image = [image stretchableImageWithLeftCapWidth:0 topCapHeight:1];
    UIImageView *bgimageView = [[UIImageView alloc]initWithFrame:_headView.bounds];
    bgimageView.image = image;
    [_headView addSubview:bgimageView];
    
    //top
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _indexColleeciton = [[indexCollectionView alloc]initWithFrame:CGRectMake(0, 0, w, kIndexCollectionViewH) collectionViewLayout:layout];
    [_headView addSubview:_indexColleeciton];
    [self addSubview:_headView];
    
    //button
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake((w-20)/2, kHeaderViewH-25 , 20, 20)];
    button.tag = 100;
    [button setImage:[UIImage imageNamed: @"down_home"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:  @"up_home" ]forState:UIControlStateSelected];
    [button addTarget:self action:@selector(pullAction:) forControlEvents:UIControlEventTouchUpInside];
    [_headView addSubview:button];
}

- (void)_createPostView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection =  UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;

    _postCollectionView = [[posterCollectionView alloc]initWithFrame:CGRectMake(0, kPosterColletionViewY, w, kPosterColletionViewH) collectionViewLayout:layout];
    _postCollectionView.backgroundColor = [UIColor clearColor];
    [self addSubview:_postCollectionView];
}

- (void)_createBottomLabel{
    _bottomTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, kBottomTitleLableY-64, w, kBottomTitleLableH)];
    _bottomTitleLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:_bottomTitleLabel];
}

- (void)_createGesture{
    UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction)];
    swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
    [self addGestureRecognizer:swipeDown];
}

- (void)_createCoverView{
    _coverView = [[UIControl alloc]initWithFrame:self.bounds];
    _coverView.hidden = YES;
    _coverView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.8];
    [_coverView addTarget:self action:@selector(coverAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_coverView];
}

- (void)swipeAction{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    _headView.top = navH;
    _coverView.hidden = NO;
    UIButton *button  = (UIButton*)[_headView viewWithTag:100];
    button.selected =!button.selected;
    [UIView commitAnimations];
}

- (void)coverAction:(UIControl*)control{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    _headView.top = -kHeaderOffH;
    _coverView.hidden = YES;
    UIButton *button  = (UIButton*)[_headView viewWithTag:100];
    button.selected =!button.selected;
    [UIView commitAnimations];
}

- (void)pullAction:(UIButton *)button{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    if (button.selected ==NO) {
        _headView.top = navH;
        _coverView.hidden = NO;
    }
    else{
        _headView.top = -kHeaderOffH;
        _coverView.hidden = YES;
    }
    button.selected = !button.selected;
    [UIView commitAnimations];

}

- (void)setMovieModelArray:(NSArray *)movieModelArray{
    
    _movieModelArray = movieModelArray;
    
    _postCollectionView.movieModelArray= _movieModelArray;
    _indexColleeciton.movieModelArray = _movieModelArray;
}


@end
